#include "SystemOperation.h"

namespace Common
{
	SystemOperation::SystemOperation(void)
	{
	}

	SystemOperation::~SystemOperation(void)
	{
	}

	//Get the running .exe file name without path
	string SystemOperation::GetProgrammeName()
	{
		//Get the module full path
		wchar exeFullPath[MAX_PATH];
		::GetModuleFileNameW(NULL, exeFullPath, MAX_PATH);

		//Get the name
		string strExeName = exeFullPath;
		strExeName = strExeName.substr(strExeName.find_last_of(TEXT("\\")) + 1, strExeName.length());

		return strExeName;
	}

	//Get the running .exe file full path
	string SystemOperation::GetProgrammePath()
	{
		//Get the module full path
		wchar exeFullPath[MAX_PATH];
		::GetModuleFileNameW(NULL, exeFullPath, MAX_PATH);

		return string(exeFullPath);
	}

	//Get the running .exe file dictionary
	string SystemOperation::GetProgrammeDictionary()
	{
		//Get the module full path
		wchar exeFullPath[MAX_PATH];
		::GetModuleFileNameW(NULL, exeFullPath, MAX_PATH);

		//Get the name
		string strExeName = exeFullPath;
		strExeName = strExeName.substr(0, strExeName.find_last_of(TEXT("\\")));

		return strExeName;
	}

	//Get the current working dictionary
	string SystemOperation::GetWorkingDictionary()
	{
		//Get the current dictionary
		wchar currentDir[MAX_PATH];
		::GetCurrentDirectoryW(MAX_PATH, currentDir);

		return string(currentDir);
	}

	//Get current process handle in text
	string SystemOperation::GetCurrentProcessHandleText()
	{
		HANDLE hProcess = ::GetCurrentProcess();

		string wstrDest;
		if (Convert::ToString(hProcess, wstrDest) != R_SUCCEED)
		{
			return string();
		}

		return wstrDest;
	}

	//Get current process id in text
	string SystemOperation::GetCurrentProcessIdText()
	{
		DWORD dwProcessId = ::GetCurrentProcessId();

		string wstrDest;
		if (Convert::ToString(dwProcessId, wstrDest) != R_SUCCEED)
		{
			return string();
		}

		return wstrDest;

		return string();
	}

	//Get current process handle in text
	string SystemOperation::GetCurrentThreadHandleText()
	{
		HANDLE hThread = ::GetCurrentThread();

		string wstrDest;
		if (Convert::ToString(hThread, wstrDest) != R_SUCCEED)
		{
			return string();
		}

		return wstrDest;
	}

	//Get current process id in text
	string SystemOperation::GetCurrentThreadIdText()
	{
		DWORD dwThreadId = ::GetCurrentThreadId();

		string wstrDest;
		if (Convert::ToString(dwThreadId, wstrDest) != R_SUCCEED)
		{
			return string();
		}

		return wstrDest;
	}

	//Find file function
	void SystemOperation::FindFile(string fileName, FindFileCallBackFunc pFindFunc /*= NULL*/)
	{
		WIN32_FIND_DATA findData;
		string curFileName;
		HANDLE hFindFile = FindFirstFile(fileName.c_str(), &findData);

		if (hFindFile == INVALID_HANDLE_VALUE)
		{
			return;
		}

		while (FindNextFile(hFindFile, &findData))
		{
			curFileName = findData.cFileName;

			if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && curFileName != L"." && curFileName != L"..")
			{
				//Get find file dir path
				string dirName = fileName.substr(0, fileName.rfind(L'\\') + 1);
				dirName += findData.cFileName;

				if (pFindFunc != NULL)
				{
					pFindFunc(dirName, findData);
				}

				dirName += L"\\*.*";

				FindFile(dirName, pFindFunc);
			}
			else if (curFileName == L"." || curFileName == L"..")
			{
				continue;
			}
			else
			{
				if (pFindFunc != NULL)
				{
					string curFileFullName = fileName.substr(0, fileName.rfind(L'\\') + 1);
					curFileFullName += findData.cFileName;

					pFindFunc(curFileFullName, findData);
				}
			}
		}

		FindClose(hFindFile);

	}
}

